#include "gifimagedescriptor.h"

namespace WingGif {

GifImageDescriptor::GifImageDescriptor(QIODevice *stream, QObject *parent)
    : QObject(parent) {
  auto bytes = stream->read(9);
  auto pbyte = bytes.data();
  _left = *reinterpret_cast<quint16 *>(pbyte);
  _top = *reinterpret_cast<quint16 *>(pbyte + 2);
  _width = *reinterpret_cast<quint16 *>(pbyte + 4);
  _height = *reinterpret_cast<quint16 *>(pbyte + 6);
  auto packedFields = bytes[8];
  _hasLocalColorTable = (packedFields & 0x80) != 0;
  _interlace = (packedFields & 0x40) != 0;
  _isLocalColorTableSorted = (packedFields & 0x20) != 0;
  _localColorTableSize = 1 << ((packedFields & 0x07) + 1);
}

int GifImageDescriptor::left() { return _left; }

int GifImageDescriptor::top() { return _top; }

int GifImageDescriptor::width() { return _width; }

int GifImageDescriptor::height() { return _height; }

bool GifImageDescriptor::hasLocalColorTable() { return _hasLocalColorTable; }

bool GifImageDescriptor::interlace() { return _interlace; }

bool GifImageDescriptor::isLocalColorTableSorted() {
  return _isLocalColorTableSorted;
}

int GifImageDescriptor::localColorTableSize() { return _localColorTableSize; }

} // namespace WingGif
